/**
*	Function: parseCData
*
*	Gets an element tag and extracts it's CData.
*
*	@param tag - the tag name of the extracted element.
*	@param xmlDoc - the document to extract the data
*
*	@return String - CData of element
*/
function parseCData(tag, xmlDoc){

	if(xmlDoc.getElementsByTagName(tag).length > 0){
		return xmlDoc.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
	}else{
		return "";
	}	
};

/**
*	Function: parseNumData
*
*	Gets an element tag, extracts it's CData and transforms it in a integer number.
*
*	@param tag - the tag name of the extracted element.
*	@param xmlDoc - the document to extract the data
*
*	@return int - numerical value from element
*/
function parseNumData(tag, xmlDoc){
	return parseInt(parseCData(tag, xmlDoc));
};

/**
*	Function: parseNumAttr
*
*	Gets an numerical element attribute.
*
*	@param tag - the name of the extracted attribute.
*	@param xmlDoc - the document to extract the data
*	
*	@return int - numerical value of attribute
*/
function parseNumAttr(attr, xmlDoc){
	return parseInt(xmlDoc.getAttribute(attr));
};

/**
*	Functon: parseDateXml
*	
*	Get date element attribute of xml document
*	
*	@return date - contains year, month, day 
*/
function parseDateXml(xml){
	
	var day = $(xml).find(TAG_DAY).text();
	var month = $(xml).find(TAG_MONTH).text();
	var year = $(xml).find(TAG_YEAR).text();
	
	var date = new Date(parseInt(year), parseInt(month), parseInt(day));
	
	return date;
	
};

/**
*	Functon: parseProjectDateXml
*	
*	Get project date element attribute of xml document
*	
*	@return date - contains year, month, day 
*/
function parseProjectDateXml(tag, xmlDoc){
	
	var date = xmlDoc.getElementsByTagName(tag)[0];
	
	var day = parseCData(TAG_DAY, date);
	var month = parseCData(TAG_MONTH, date);
	var year = parseCData(TAG_YEAR, date);
				
	return new Date(year, month, day);
	
};


/**
*	Function: parsePostsXml
*
*	Get collection of post-its of certain user from xml document
*
*	@return array of post-its
*/
function parsePostsXml(xml) {

	var posts = new Array(0);
	
	$(xml).find(TAG_POST_IT).each(function(){
		var postIt = new PostIt();
	
		postIt.setId(parseInt($(this).attr(TAG_ID))); 
		postIt.setTask($(this).find(TAG_TASK).text());
		postIt.setDescription($(this).find(TAG_DESCRIPTION).text());
		postIt.setStars($(this).find(TAG_STARS).text());
	
		var startDate = parseDateXml($(this).find(TAG_START_DATE));
		
		postIt.setStartDate(startDate);			
		
		if($(this).find(TAG_FINALIZATION_DATE).length > 0){			
			postIt.setFinalizationDate(parseDateXml($(this).find(TAG_FINALIZATION_DATE)));			
		}
		
		if($(this).find(TAG_EXPIRATION_DATE).length > 0){			
			postIt.setExpirationDate(parseDateXml($(this).find(TAG_EXPIRATION_DATE)));			
		}
		
		posts.push(postIt);
	});
	
	return posts;
};


/**
*	Function: parseProjectXml
*
*	Get collection of projects 	of certain user from xml document
*
*	@return array of projects 
*/
function parseProjectXml(xml) {
	var projects = new Array(0);


	$(xml).find(TAG_PROJECT).each(function(){

		var name = $(this).find(TAG_NAME).text();	
		
		var startDate = parseProjectDateXml(TAG_START_DATE, xml);
		var finalizationDate = parseProjectDateXml(TAG_FINALIZATION_DATE, xml);
					
		var project = new Project(name, startDate, finalizationDate); 
				
		$(this).find(TAG_TODO).each(function(){
			
			var posts = parsePostsXml(this);
			
			var todo = new Container(TAG_TODO);
			todo.addPosts(posts);
			project.setTodo(todo);
		});
		$(this).find(TAG_DOING).each(function(){
			var posts = parsePostsXml(this);
			var doing = new Container(TAG_DOING);
			doing.addPosts(posts);
			project.setDoing(doing);
		});
		$(this).find(TAG_IMPEDED).each(function(){
			var posts = parsePostsXml(this);
			var impeded = new Container(TAG_IMPEDED);
			impeded.addPosts(posts);
			project.setImpeded(impeded);
		});
		$(this).find(TAG_DONE).each(function(){
			var posts = parsePostsXml(this);
			var done = new Container(TAG_DONE);
			done.addPosts(posts);
			project.setDone(done);
		});

		projects.push(project);
	
	});
	
	return projects;
};


function parseUserXml(xml){
		
	var user = new User();
	user._nextId = parseInt($(xml).find(TAG_NEXT_ID).text());
		
	
	var projects = new Array(0);
	projects = parseProjectXml(xml);

	user.projects = projects;	
	
	return user;
};


/*

function test(){

	var xmlStr = new String("<castor><user>\
								<nextId>5</nextId>\
								<project>\
								<name>Example Project</name>\
								<startDate>\
									<day>20</day>\
									<month>03</month>\
									<year>1999</year>\
								</startDate>\
								<finalizationDate>\
									<day>12</day>\
									<month>11</month>\
									<year>1999</year>\
								</finalizationDate>\
								<todo>\
									<postIt id=\"10\">\
										<task>Algo a ser feito ainda hj</task>\
										<description>lolol</description>\
										<stars>1</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
									</postIt>\
								</todo>\
								<doing>\
									<postIt id=\"11\">\
										<task>Algo a ser feito</task>\
										<description>whatisluv</description>\
										<stars>1</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<finalizationDate>\
											<day>01</day>\
											<month>10</month>\
											<year>2009</year>\
										</finalizationDate>\
									</postIt>\
									<postIt id=\"12\">\
										<task>IniciarProva</task>\
										<description></description>\
										<stars>3</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>05</day>\
											<month>11</month>\
											<year>2009</year>\
										</expirationDate>\
									</postIt>\
								</doing>\
								<impeded>\
									<postIt id=\"13\">\
										<task>Algo a ser feito</task>\
										<description>whatisluv</description>\
										<stars>1</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<finalizationDate>\
											<day>01</day>\
											<month>10</month>\
											<year>2009</year>\
										</finalizationDate>\
									</postIt>\
									<postIt id=\"14\">\
										<task>IniciarProva</task>\
										<description></description>\
										<stars>3</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>05</day>\
											<month>11</month>\
											<year>2009</year>\
										</expirationDate>\
									</postIt>\
								</impeded>\
							</project>\
							<project>\
								<name>Example Project</name>\
								<startDate>\
									<day>20</day>\
									<month>03</month>\
									<year>1999</year>\
								</startDate>\
								<finalizationDate>\
									<day>12</day>\
									<month>11</month>\
									<year>1999</year>\
								</finalizationDate>\
								<todo>\
									<postIt id=\"15\">\
										<task>Algo a ser feito ainda hj</task>\
										<description>lolol</description>\
										<stars>1</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
									</postIt>\
								</todo>\
								<doing>\
									<postIt id=\"16\">\
										<task>Algo a ser feito</task>\
										<description>whatisluv</description>\
										<stars>1</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<finalizationDate>\
											<day>01</day>\
											<month>10</month>\
											<year>2009</year>\
										</finalizationDate>\
									</postIt>\
									<postIt id=\"17\">\
										<task>IniciarProva</task>\
										<description></description>\
										<stars>3</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>05</day>\
											<month>11</month>\
											<year>2009</year>\
										</expirationDate>\
									</postIt>\
								</doing>\
								<impeded>\
									<postIt id=\"18\">\
										<task>Impedimento</task>\
										<description>Descricao</description>\
										<stars>3</stars>\
										<startDate>\
											<day>15</day>\
											<month>06</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>12</day>\
											<month>10</month>\
											<year>2009</year>\
										</expirationDate>\
										<finalizationDate>\
											<day>25</day>\
											<month>1</month>\
											<year>2010</year>\
										</finalizationDate>\
									</postIt>\
								</impeded>\
								<done>\
									<postIt id=\"19\">\
										<task>Done</task>\
										<stars>2</stars>\
										<startDate>\
											<day>31</day>\
											<month>3</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>3</day>\
											<month>3</month>\
											<year>2010</year>\
										</expirationDate>\
										<finalizationDate>\
											<day>30</day>\
											<month>3</month>\
											<year>2010</year>\
										</finalizationDate>\
									</postIt>\
								</done>\
							</project>\
							<project>\
								<name>Example Project</name>\
								<startDate>\
									<day>22</day>\
									<month>3</month>\
									<year>2019</year>\
								</startDate>\
								<finalizationDate>\
									<day>11</day>\
									<month>15</month>\
									<year>2020</year>\
								</finalizationDate>\
								<todo>\
									<postIt id=\"20\">\
										<task>Algo a ser feito ainda hj</task>\
										<description>lolol</description>\
										<stars>1</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
									</postIt>\
								</todo>\
								<doing>\
									<postIt id=\"5\">\
										<task>Algo a ser feito</task>\
										<description>whatisluv</description>\
										<stars>1</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<finalizationDate>\
											<day>01</day>\
											<month>10</month>\
											<year>2009</year>\
										</finalizationDate>\
									</postIt>\
									<postIt id=\"21\">\
										<task>IniciarProva</task>\
										<description></description>\
										<stars>3</stars>\
										<startDate>\
											<day>10</day>\
											<month>10</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>05</day>\
											<month>11</month>\
											<year>2009</year>\
										</expirationDate>\
									</postIt>\
								</doing>\
								<impeded>\
									<postIt id=\"22\">\
										<task>Impedimento</task>\
										<description>Descricao</description>\
										<stars>3</stars>\
										<startDate>\
											<day>15</day>\
											<month>06</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>12</day>\
											<month>10</month>\
											<year>2009</year>\
										</expirationDate>\
										<finalizationDate>\
											<day>25</day>\
											<month>1</month>\
											<year>2010</year>\
										</finalizationDate>\
									</postIt>\
								</impeded>\
								<done>\
									<postIt id=\"23\">\
										<task>Done</task>\
										<stars>2</stars>\
										<startDate>\
											<day>31</day>\
											<month>3</month>\
											<year>2009</year>\
										</startDate>\
										<expirationDate>\
											<day>3</day>\
											<month>3</month>\
											<year>2010</year>\
										</expirationDate>\
										<finalizationDate>\
											<day>30</day>\
											<month>3</month>\
											<year>2010</year>\
										</finalizationDate>\
									</postIt>\
								</done>\
							</project>\
							</user>\
							</castor>\
							");
	

	document.write(xmlStr);parseUserXml
	var xmlDoc = (new DOMParser()).parseFromString(xmlStr, "text/xml");
	//var project = new Array;
	//project = parseProjectXml(xmlDoc);
	
	//document.write(project[0].genXml());
	/*for (var i = 0 ;  i < project.length; i++){
	
		document.write(project[i].genXml());
		
	} 
	
	
	var user = parseUserXml(xmlDoc);
	document.write(user.genXml());
		
};*/
